<template>
  <div class="header">
    <el-row>
      <el-col :span="5">
        <!-- <img class="logo_img" src="/logo.jpg" title="饿了吗~" height="60"> -->
        <span class="title">视频后台管理系统</span>
      </el-col>
      <el-col :span="16">

      </el-col>
      <el-col :span="3">
        <div class="status">
          <div class="role">
            <el-dropdown>
              <div class="dropdown">
                <el-avatar :src="adminInfo.userAvatar" :size="60" style="margin-right: 15px"></el-avatar>
                <div>{{ adminInfo.username }}</div>
              </div>
              <template #dropdown>
                <el-dropdown-menu>

                  <el-dropdown-item @click="handleUpdatePass">修改密码</el-dropdown-item>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>

                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>

  <el-dialog
      v-model="updatePasswordDialogVisible"
      title="修改密码"
      width="500"
  >
   <el-form :model="updatePwdForm" ref="ruleFormRef" label-width="auto" :rules="rules" style="max-width: 500px">
     <el-form-item label="密码" prop="password">
       <el-input type="password" v-model="updatePwdForm.password" placeholder="请输入密码"></el-input>
     </el-form-item>
     <el-form-item label="确认密码" prop="secondPass">
       <el-input type="password" v-model="updatePwdForm.secondPass" placeholder="请再次输入密码"></el-input>
     </el-form-item>
   </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="updatePasswordDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {onMounted, reactive, ref} from 'vue'
import {useEmployeeStore} from "@/stores/adminStore.js";
import {useRouter} from "vue-router";
import {updatePassword} from "@/api/admin-api.js";
import {ElMessage} from "element-plus";

/**
 * 账号状态
 */
const operation = ref()
let currentRole = ref('管理员')

const options = [
  {
    value: 1,
    label: '修改密码',
  },
  {
    value: 2,
    label: '退出登录',
  },
]

const selectIt = (value) => {
  console.log(value);
}

const employeeStore = useEmployeeStore()
const adminInfo = ref(employeeStore.employeeInfo)
// console.log(employeeStore.employeeInfo)
// adminInfo.value = employeeStore.employeeInfo
onMounted(()=>{

})

/****** 修改密码相关 ******/

const ruleFormRef = ref(null)
const rules = reactive({
  password: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 6, max: 10, message: '密码长度必须在6-10之间', trigger: 'blur' },
  ],
  secondPass: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 6, max: 10, message: '密码长度必须在6-10之间', trigger: 'blur' },
  ],

})

const updatePasswordDialogVisible = ref(false)
const updatePwdForm = reactive({
  password:'',
  secondPass:''
})

const handleUpdatePass = () => {
  updatePasswordDialogVisible.value = true
}

const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      await doUpdate()
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}


const doUpdate = async () => {
  try {
    const res = await updatePassword(updatePwdForm)
    console.log(res);
    ElMessage.success("密码修改成功")
    updatePasswordDialogVisible.value = false
  }catch (e){
    ElMessage.error("密码修改失败"+e.message)
  }
}


/****** 退出登录相关 ******/
const router = useRouter()
const handleLogout = async ()=>{
  localStorage.removeItem("auth")
  useEmployeeStore().setEmployeeInfo({})
  router.push('/admin/login')
}



</script>
<style lang="scss" scoped>
.header{
  background-color: deepskyblue;
  width: 100%;
  height: 60px;
  border-top-right-radius: 10px;
  border-top-left-radius: 10px;
  // .el-row{
  //     border-radius: 60px;
  // }

  .logo_img{
    height:60px;
    margin-left: 60px;

  }
  .title{
    //width: 300px;
    padding-left: 20px;
    font-family: 楷体;
    font-size: 20px;
    height: 60px;
    line-height: 60px;
    color: #000000;
    box-sizing: border-box;
  }
  .status{
    height: 60px;
    width: 100%;
    display: flex;
    //background-color: blue;
    justify-content: space-around;
    align-items: center;
    .trade_status{
      height: 60px;
      line-height: 60px;
      cursor: pointer;
      .desc{
        font-size: 15px;
      }
    }
    .role:hover{
      //background-color: #fff;
      border-radius: 5px;
    }
  }
  .status_show{
    height: 60px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    color: white;
    font-weight: bold;
  }
  .dropdown{
    width: 180px;
    padding: 10px;
    height: 60px;
    //height: 30px;
    //line-height: 30px;
    text-align: center;
    //background-color: #c0c7cb;
    //border: 1px solid rgb(9, 0, 42);
    border-radius: 5px;
    display: flex;
    align-items: center;
    //justify-content: space-between;
    font-size: 16px;
  }

}
</style>
